<?php
namespace app\index\controller;
use think\Controller;
use think\Session;
class UserIndex extends Mycontroller
{

    public function __construct(){
        parent::__construct();
        //
    }


    public function index(){
        return $this->fetch('homePage/userPage');
    }


   public function goadduser($id){
        //发送部门名称
        $classinfo = db('class')->where('id','=',$id)->find();
        $this->assign('classinfo',$classinfo);

        return $this->fetch('userindex/adduser');

    }


    public function adduser(){
        //验证所有信息都不为空
        foreach ($_POST as $v){
            if(empty($v)){
                echo  "<script>alert('信息输入有误!');history.back()</script>";
                return ;
            }
        }
        //判定密码
        if($_POST['pass']!==$_POST['repass']){
            echo  "<script>alert('两次密码输入不一致!');history.back()</script>";
            return ;
        }else{
            unset($_POST['repass']);
        }
        //判定局长
        $empty = db('user')->where('position','=','局长')->where('class','=',$_POST['class'])->find();
        if($empty &&($_POST['position']=='局长')){
             echo  "<script>alert('请勿重复添加局长职位');history.back()</script>";
            return ;
        }
        //判定人员唯一
        $person = db('user')->where('name','=',$_POST['name'])->where('class','=',$_POST['class'])->find();
        if($person){
             echo  "<script>alert('姓名重复');history.back()</script>";
            return ;
        }
        //插入信息
        $res = db('user')->insert($_POST);
        if($res){
            $id = $_POST['class'];
            $classid = $this->assign('classid',$id);
            //查询该部门人员信息 发送前态
            $userlist = db('user')->where('class','=',$id)->select();
            $classname = db('class')->where('id','=',$id)->value('name');
             $this->assign('userlist',$userlist);
             $this->assign('classname',$classname);
            echo  "<script>alert('添加成功');</script>";
            return action('UserIndex/index');
        }

             echo  "<script>alert('操作失败');history.back()</script>";
            return ;
    }
   
    public function del($id){
        $res = db('user')->delete($id);
        if($res){
            echo  "<script>alert('删除成功');</script>";
        }else{
            echo  "<script>alert('删除失败');</script>";
        }
         return action('UserIndex/index');
    }


    public function goeditposition($id){
         //查询员工信息
        $userinfo =  db('user')->where('id','=',$id)->find();
        $this->assign('userinfo',$userinfo);

        //查询部门信息
        $classname  =db('class')->where('id','=',$userinfo['class'])->value('name');
        $this->assign('classname',$classname);

        return $this->fetch('UserIndex/editposition');
        
    }

    public function editposition(){
            $data=[
            'id'=>$_POST['id'],
            'position'=>$_POST['position']
            ];
        $empty = db('user')->where('position','=','局长')->where('class','=',$_POST['id'])->find();
        if($empty &&($_POST['position']=='局长')){
             echo  "<script>alert('请勿重复添加局长职位');history.back()</script>";
            return ;
        }
        $res = db('user')->update($data);
            // var_dump($res);die;
        if($res){
            echo  "<script>alert('修改成功');</script>";
            $classList = db('class')->select();
            $this->assign('classList',$classList);
            return action('UserIndex/index');
        }

             echo  "<script>alert('修改失败');history.back()</script>";
            return ;

    }

}
